package com.qf.electronic.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.electronic.dto.DefectTaskDto;
import com.qf.electronic.dto.InspectionReceptionStateDto;
import com.qf.electronic.pojo.DefectTask;
import com.qf.electronic.mapper.DefectTaskMapper;
import com.qf.electronic.pojo.User;
import com.qf.electronic.service.IDefectTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.electronic.vo.DefectTaskVo;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wu
 * @since 2023-05-04
 */
@Service
public class DefectTaskServiceImpl extends ServiceImpl<DefectTaskMapper, DefectTask> implements IDefectTaskService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addDefectTask(InspectionReceptionStateDto stateDto) {
        DefectTask task = new DefectTask();
        task.setName(stateDto.getDefectTaskName());
        task.setDescription(stateDto.getDefectTaskDesc());
        task.setInspectionReceptionId(stateDto.getId());
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        task.setCreatedUser(user.getUsername());
        return save(task);
    }

    @Transactional(readOnly = true, isolation = Isolation.READ_COMMITTED)
    @Override
    public Page<DefectTaskVo> getDefectTasks(DefectTaskDto taskDto) {
        Page<DefectTask> page = new Page<>(taskDto.getCurrentPage(), taskDto.getPageSize());
        return this.baseMapper.getDefectTasks(page, taskDto);
    }
}
